package org.baitiaojun.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import org.baitiaojun.pojo.CheckGroup;
import org.baitiaojun.pojo.CheckGroupCheckItemKey;
import org.baitiaojun.pojo.CheckItem;
import org.baitiaojun.service.CheckGroupCheckItemService;
import org.baitiaojun.service.CheckGroupService;
import org.baitiaojun.service.CheckItemService;
import org.baitiaojun.util.MessageConstant;
import org.baitiaojun.vo.PageResult;
import org.baitiaojun.vo.QueryPageBean;
import org.baitiaojun.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @类名 CheckGroupController
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2022/5/25 19:40
 * @版本 1.0
 */
@Controller
@RequestMapping("/pages")
public class CheckGroupController {

    @Reference(interfaceClass = CheckItemService.class)
    private CheckItemService checkItemService;

    @Reference(interfaceClass = CheckGroupService.class)
    private CheckGroupService checkGroupService;

    @Reference(interfaceClass = CheckGroupCheckItemService.class)
    private CheckGroupCheckItemService checkGroupCheckItemService;

    @GetMapping("/queryCheckItemList.do")
    public @ResponseBody Result queryCheckItemList() {
        List<CheckItem> checkItems = checkItemService.queryCheckItemList();
        Result result = new Result();
        if (!ObjectUtils.isEmpty(checkItems)) {
            result.setFlag(true);
            result.setData(checkItems);
        } else {
            result.setFlag(false);
            result.setMessage(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return result;
    }

    @PostMapping("/saveCheckGroup.do")
    public @ResponseBody Result saveCheckGroup(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds)  {
        Result result = new Result();
        try {
            checkGroupService.saveCheckGroup(checkGroup, checkitemIds);
            result.setFlag(true);
            result.setMessage(MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return result;
    }

    @PostMapping("/splitPageByCondition.do")
    public @ResponseBody PageResult<CheckGroup> splitPageByCondition(@RequestBody QueryPageBean queryPageBean) {
        return checkGroupService.queryCheckGroupByPageAndCondition(queryPageBean);
    }

    @GetMapping("/queryCheckGroupList.do")
    public @ResponseBody Result queryCheckGroupList(@RequestParam Integer id) {
        CheckGroup groups =  checkGroupService.queryCheckGroupList(id);
        Result result = new Result();
        if (!ObjectUtils.isEmpty(groups)) {
            result.setFlag(true);
            result.setData(groups);
        } else {
            result.setFlag(false);
        }
        return result;
    }

    @GetMapping("/queryCheckGroup.do")
    public @ResponseBody Result queryCheckGroup(@RequestParam Integer id) {
        CheckGroup groups =  checkGroupService.queryCheckGroupList(id);
        Result result = new Result();
        if (!ObjectUtils.isEmpty(groups)) {
            result.setFlag(true);
            result.setData(groups);
        } else {
            result.setFlag(false);
            result.setMessage(MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return result;
    }

    @GetMapping("/queryCheckItem.do")
    public @ResponseBody Result queryCheckItem() {
        List<CheckItem> checkItems = checkItemService.queryCheckItemList();
        Result result = new Result();
        if (!ObjectUtils.isEmpty(checkItems)) {
            result.setFlag(true);
            result.setData(checkItems);
        } else {
            result.setFlag(false);
            result.setMessage(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return result;
    }

    @GetMapping("/queryCheckGroupIdList.do")
    public @ResponseBody Result queryCheckGroupIdList(@RequestParam Integer id) {
        List<Integer> checkItemIds = checkGroupCheckItemService.queryCheckItemIdsById(id);
        return new Result(true, checkItemIds);
    }

    @PostMapping("/editCheckGroup.do")
    public @ResponseBody Result editCheckGroup(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        Result result = new Result();
        try {
            checkGroupService.editCheckGroup(checkGroup, checkitemIds);
            result.setFlag(true);
            result.setMessage(MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @GetMapping("/removeCheckGroup.do")
    public @ResponseBody Result removeCheckGroup(@RequestParam Integer id) {
        Result result = new Result();
        try {
            checkGroupService.removeCheckGroup(id);
            result.setFlag(true);
            result.setMessage(MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return result;
    }
}